<template>
  <div class="p-2" style="padding: 0px 5px">
    <el-input v-if="props.searchType === 1" :style="{ width: '200px'}" readonly v-model="pd.dataText" :placeholder="props.title">
      <template #prepend>
        <el-button icon="Search" @click="initSearch"></el-button>
      </template>
    </el-input>
    <el-button v-if="props.searchType === 2" type="primary" @click="initSearch">{{ props.dataText }}</el-button>
    <el-link v-if="props.searchType === 3" type="primary" @click="initSearch">{{ pd.dataText }}</el-link>
    <el-link v-if="props.searchType === 4" type="primary" @click="initSearch">{{ props.dataText }}</el-link>
    <el-dialog :title="props.title" v-model="pd.open" width="900px" append-to-body>
      <el-form :model="pd.queryParams" ref="queryFormRef" :inline="true">
        <el-form-item label="样例名称" prop="nickName">
          <el-input v-model="pd.queryParams.nickName" placeholder="请输名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜 索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重 置</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <el-table v-loading="pd.loading" @select="select1" @select-all="selectAll" ref="refTable" :data="pd.userList" height="260px">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="主键" align="center" prop="id" v-if="false" />
          <el-table-column label="昵称" align="center" prop="nickName" />
          <el-table-column label="性别" align="center" prop="sex">
            <template #default="scope">
              <dict-tag :options="sys_user_sex" :value="scope.row.sex" />
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="status">
            <template #default="scope">
              <dict-tag :options="sys_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="邮箱" align="center" prop="email" />
          <el-table-column label="部门" align="center" prop="dept.deptName" />
          <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
            <template #default="scope">
              <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                  v-model:limit="pd.queryParams.pageSize" @pagination="getList" />
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSelected">确 定</el-button>
          <el-button @click="pd.open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="UserSearch" lang="ts">
import {listUser} from '@/api/system/user';
import {UserVO} from '@/api/system/user/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_status,sys_user_sex } = toRefs<any>(proxy?.useDict('sys_status','sys_user_sex'));

const queryFormRef = ref<ElFormInstance>();
const props = defineProps({
  //组件类别。1是输入搜索框，2是按钮搜索框，3link文本样式弹出框，默认是1样式。
  searchType: {type: Number, default: 1,},
  // 对话框标题
  title: {type: String, default: "样例查询"},
  // 是否单选
  isSingle: {type: Boolean, default: true},
  // 有监控，只要变化，控件自主修改
  dataText: {type: String, default: ''},
  // 数据过滤参数// 用于1、2样式的
  params: {type: Object, default: null}
});

const pd = reactive({
  userList: <UserVO[]>([]),
  reParams: <UserVO[]>([]),
  loading: true,
  total: 0,
  open: false,
  dataText: '',
  queryParams: <UserVO>{
    pageNum: 1,
    pageSize: 10,
    deptId: undefined,
    userId: undefined,
  }
});

function select1(selection: UserVO[], row: UserVO[]) {
  //是否为单选模式
  if (props.isSingle) {
    //清空选项
    (proxy?.$refs["refTable"] as any).clearSelection();
  }
  //将这行选中
  (proxy?.$refs["refTable"] as any).toggleRowSelection(row, true);
}

function selectAll(selection: UserVO[]) {
  //是否为单选模式
  if (props.isSingle) {
    //清空选项
    (proxy?.$refs["refTable"] as any).clearSelection();
  }
}

/** 查询测试单列表 */
async function getList() {
  pd.loading = true;
  const res = await listUser(pd.queryParams);
  pd.userList = res.rows;
  pd.total = res.total;
  pd.loading = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields();
  handleQuery();
}

defineExpose({
  initSearch
});

const emit = defineEmits(["ok"]);

//数据变化页面需要变化,默认null
watch(() => props.dataText, val => {
  if (props.searchType === 2 || props.searchType === 4 ){
    return;
  }
  if (props.dataText == ''){
    pd.dataText = '';
  } else {
    pd.dataText = props.dataText;
  }
}, {deep: true, immediate: true});

// 初始化1样式的展示
function initSearch() {
  pd.queryParams = {...pd.queryParams, ...props.params};
  handleQuery();
  pd.open = true;
}

/** 按钮选择操作 */
function handleSelected() {
  pd.reParams = (proxy?.$refs["refTable"] as any).getSelectionRows();
  if (pd.reParams.length === 0) {
    proxy?.$modal.msgError("请选择数据，至少一行!!");
    return;
  }
  pd.reParams.forEach((v, i, a) => {
    pd.dataText = v.nickName!;
    return;
  });
  pd.open = false;
  emit("ok", pd.reParams);
}

</script>
